package com.typhoon.spring_boot_demo1.manager;

import com.typhoon.spring_boot_demo1.bean.User;
import com.typhoon.spring_boot_demo1.constants.UserOptTypeEnum;
import com.typhoon.spring_boot_demo1.exception.UserException;
import com.typhoon.spring_boot_demo1.proccessor.IOperation;
import com.typhoon.spring_boot_demo1.request.UserOptContext;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户操作调配器
 */
public class UserOptManager {

    private Map<UserOptTypeEnum,IOperation> proccessMap = new HashMap<>();

    public void operate(UserOptTypeEnum optTypeEnum,UserOptContext context)   throws UserException {
        IOperation operation = proccessMap.get(optTypeEnum);
        if(null == operation) {
            throw new UserException("执行器不存在");
        }
        context.setOperateType(optTypeEnum.getCode());
        operation.operate(context);
    }

    /**
     * 新增
     *
     * @param context
     * @throws UserException
     */
    public void insert(UserOptContext context) throws UserException {
        this.operate(UserOptTypeEnum.INSERT,context);
    }

    /**
     * 删除
     *
     * @param context
     * @throws UserException
     */
    public void delete(UserOptContext context) throws UserException {
        this.operate(UserOptTypeEnum.DELETE,context);
    }

    /**
     * 更新
     * @param context
     * @throws UserException
     */
    public void update(UserOptContext context) throws UserException {
        this.operate(UserOptTypeEnum.UPDATE,context);
    }

    public void setProccessMap(Map<UserOptTypeEnum, IOperation> proccessMap) {
        this.proccessMap = proccessMap;
    }
}
